package com.example.prison.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.VisitorDeviceInfo;
import com.example.prison.service.SystemLogService;
import com.example.prison.service.VisitorDeviceInfoService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "访客设备管理")
@RestController
@RequestMapping("/api")
public class VisitorDeviceInfoController {

    @Autowired
    private VisitorDeviceInfoService visitorDeviceInfoService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取访客设备列表")
    @GetMapping("/getVisitorDevice")
    public Response<PageInfo<VisitorDeviceInfo>> getVisitorDevice(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                  @Parameter(description = "设备编号")@RequestParam(required = false) String deviceNumber) {
        PageInfo<VisitorDeviceInfo> pageInfo = visitorDeviceInfoService.get(pageNumber, pageSize, deviceNumber);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增访客设备")
    @PostMapping("/addVisitorDevice")
    public Response<?> addVisitorDevice(@RequestBody VisitorDeviceInfo device) {
        device.setDeviceStatus("离线");
        int result = visitorDeviceInfoService.add(device);
        if (result == -1) {
            return Response.error("访客设备已存在！");
        }
        log.info("新增访客设备"+", 数据："+ JSON.toJSONString(device));
        systemLogService.add("新增访客设备");
        return Response.success("添加成功！");
    }

    @ApiOperation("删除访客设备")
    @PostMapping("/deleteVisitorDevice")
    public Response<?> deleteVisitorDevice(@Parameter(description = "设备ID") @RequestBody Integer[] ids) {
        visitorDeviceInfoService.delete(ids);
        log.info("删除访客设备");
        systemLogService.add("删除访客设备");
        return Response.success("删除成功！");
    }

    @ApiOperation("编辑访客设备")
    @PostMapping("/updateVisitorDevice")
    public Response<?> updateVisitorDevice(@RequestBody VisitorDeviceInfo device) {
        Integer id = device.getDeviceId();
        int result = visitorDeviceInfoService.update(device, id);
        if (result == -1) {
            return Response.error("访客设备编号已存在！");
        }
        log.info("编辑访客设备"+", 数据："+JSON.toJSONString(device));
        systemLogService.add("编辑访客设备");
        return Response.success("更新成功！");
    }

    @ApiOperation("依据id获取访客设备信息")
    @GetMapping("/getVisitorDeviceById")
    public Response<VisitorDeviceInfo> getVisitorDeviceById(@Parameter(description = "设备ID")@RequestParam Integer deviceId) {
        VisitorDeviceInfo device = visitorDeviceInfoService.getById(deviceId);
        if(ObjectUtils.isEmpty(device)) {
            return Response.error("此设备id不存在！");
        }
        return Response.success("获取成功！", device);
    }


}
